//
//  AlterSecurityCideViewController.m
//  Yihubang
//
//  Created by hyu on 2016/11/18.
//  Copyright © 2016年 xmy. All rights reserved.
//

#import "AlterSecurityCideViewController.h"

@interface AlterSecurityCideViewController ()<UITextFieldDelegate>
@property (nonatomic, strong)NSString *firstPassWord; //修改密码第一次输入
@property (nonatomic, strong)NSString *lastPassWord; //修改密码第二次输入
@property (nonatomic, assign)BOOL buttonState; //控制按钮显示与否
@property (nonatomic, strong)NSString *passWordStr;//当前输入的安全密码
@end

@implementation AlterSecurityCideViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    if ([UserClient sharedUserClient].password.length == 4) {
        [self.complete_button setHidden:YES];
        [self.skip_button setHidden:YES];
        self.navigationItem.title =@"输入安全码";
    }else{
        [self.complete_button setHidden:NO];
        self.navigationItem.title =@"设置安全码";
        [self.complete_button addTarget:self action:@selector(checkPasswordComplete) forControlEvents:UIControlEventTouchUpInside];
    }
    self.passWordStr = @"";
    self.firstPassWord = @"";
    self.lastPassWord = @"";
    self.inputTextField.maxLenght = 4;
    [self.inputTextField becomeFirstResponder];
    @weakify(self)
    RACSignal * validLoginSignal;
    if([UserClient sharedUserClient].password.length != 4){
        validLoginSignal = [[RACSignal
                             combineLatest:@[ RACObserve(self, firstPassWord),
                                              RACObserve(self, lastPassWord) ]
                             reduce:^(NSString *firstPassWord, NSString *lastPassWord) {
                                 return @(firstPassWord.length == 4 && lastPassWord.length ==4);
                             }]
                            distinctUntilChanged];
        RAC(self.complete_button, enabled) = validLoginSignal;
        [validLoginSignal
         subscribeNext:^(NSNumber * usernameIsValid) {
             @strongify(self);
             if(usernameIsValid.boolValue){
                 [self.complete_button setImage:[UIImage imageNamed:@"complete-after"] forState:UIControlStateNormal];
             }else{
                 [self.complete_button setImage:[UIImage imageNamed:@"complete-before"] forState:UIControlStateDisabled];
             }
         }];
    }else{
        validLoginSignal = [self.inputTextField.rac_textSignal
                            filter:^BOOL(NSString *password) {
                                return password.length == 4;
                            }];
        [validLoginSignal
         subscribeNext:^(NSNumber * usernameIsValid) {
             @strongify(self);
             [self checkPasswordComplete];
             
         }];
    }
    [self.inputTextField.rac_textSignal subscribeNext:^(NSString *x) {
        @strongify(self)
        NSLog(@"%@",x);
        if([UserClient sharedUserClient].password.length != 4){
            if ([x length] == 4) {
                if (![self.firstPassWord isEqualToString:@""]) {
                    self.lastPassWord = x;
                }else{
                    self.firstPassWord = x;
                    self.passWordStr = @"";
                    x = @"";
                    self.inputCodeLabel.text = @"再次输入安全密码";
                    self.inputTextField.text = @"";
                }
            }
        }
        [self passWordState:x];
        self.passWordStr = x ;
    }];
}

-(void)cancelHelp:(NSString*)str{
    str = [str substringWithRange:NSMakeRange(0, 4)];
    if ([str isEqualToString:self.userClient.password]) {
        self.inputTextField.text= @"";
        [self.inputTextField resignFirstResponder];
        //        [self.navigationController popViewControllerAnimated:NO];
        [SharedAppDelegate openAppMainVCT];
        //        [self dismissViewControllerAnimated:YES completion:^{
        //            [self.delegate checkSafePasswordSuccess];
        //        }];
    }else{
        self.inputTextField.text= @"";
        [self hiddenHUDWithString:@"安全码错误" error:NO];
    }
}

-(void)passWordState:(NSString*)str{
    switch (str.length) {
        case 0:
            [self.circleView1 setTitle:@"" forState:UIControlStateNormal];
            [self.circleView2 setTitle:@"" forState:UIControlStateNormal];
            [self.circleView3 setTitle:@"" forState:UIControlStateNormal];
            [self.circleView4 setTitle:@"" forState:UIControlStateNormal];
            break;
        case 1:
            [self.circleView1 setTitle:@"*" forState:UIControlStateNormal];
            [self.circleView2 setTitle:@"" forState:UIControlStateNormal];
            [self.circleView3 setTitle:@"" forState:UIControlStateNormal];
            [self.circleView3 setTitle:@"" forState:UIControlStateNormal];
            break;
        case 2:
            [self.circleView1 setTitle:@"*" forState:UIControlStateNormal];
            [self.circleView2 setTitle:@"*" forState:UIControlStateNormal];
            [self.circleView3 setTitle:@"" forState:UIControlStateNormal];
            [self.circleView4 setTitle:@"" forState:UIControlStateNormal];
            break;
        case 3:
            [self.circleView1 setTitle:@"*" forState:UIControlStateNormal];
            [self.circleView2 setTitle:@"*" forState:UIControlStateNormal];
            [self.circleView3 setTitle:@"*" forState:UIControlStateNormal];
            [self.circleView4 setTitle:@"" forState:UIControlStateNormal];
            break;
        case 4:
            [self.circleView1 setTitle:@"*" forState:UIControlStateNormal];
            [self.circleView2 setTitle:@"*" forState:UIControlStateNormal];
            [self.circleView3 setTitle:@"*" forState:UIControlStateNormal];
            [self.circleView4 setTitle:@"*" forState:UIControlStateNormal];
            break;
    }
}



/**
 跳过设置安全吗
 */
- (IBAction)skipSetPassWord:(id)sender {
    
    //    [SharedAppDelegate openAppMainVCT];
    UIStoryboard *story = [UIStoryboard storyboardWithName:@"LoginTo" bundle:[NSBundle mainBundle]];
    UIViewController *sexVC = [story instantiateViewControllerWithIdentifier:@"SexSelectionViewController"];
    [self.navigationController pushViewController:sexVC animated:YES];
}




- (IBAction)inputbecomeFirstReconder:(id)sender {
    [self.inputTextField becomeFirstResponder];
}
-(void)checkPasswordComplete{
    
    if ([UserClient sharedUserClient].password.length == 4){
        [self cancelHelp:self.inputTextField.text];
        
    }else{
        /* 两次密码一致 */
        if ([self.firstPassWord isEqualToString:self.lastPassWord]) {
            [self updateSecurityCode];
        }else{
            /* 两次密码不一致 */
            self.inputCodeLabel.text = @"输入安全密码";
            [self hiddenHUDWithString:@"两次安全码不一致,请重新输入" error:NO];
            self.firstPassWord = @"";
            self.lastPassWord = @"";
            self.passWordStr = @"";
            self.inputTextField.text= @"";
            [self.circleView1 setTitle:@"" forState:UIControlStateNormal];
            [self.circleView2 setTitle:@"" forState:UIControlStateNormal];
            [self.circleView3 setTitle:@"" forState:UIControlStateNormal];
            [self.circleView4 setTitle:@"" forState:UIControlStateNormal];
        }
        
    }
}


/**
 更新安全码
 */
-(void)updateSecurityCode{
    @weakify(self);
    [[[HttpManagerCenter sharedHttpManager] setSecurityCode:self.passWordStr resultClass:nil] subscribeNext:^(BaseModel *model){
        if(model.code==200){
            @strongify(self);
            [self hiddenHUDWithString:@"设置成功" error:NO];
            [self.userClient updateSecurityCode:self.passWordStr];
            if([[self.params allKeys] containsObject:@"identity"]){
                /* 判断如果是从登陆进来的  就跳转至选择性别页面 */
                UIStoryboard *stroy = [UIStoryboard storyboardWithName:@"LoginTo" bundle:[NSBundle mainBundle]];
                UIViewController *vc = [stroy instantiateViewControllerWithIdentifier:@"SexSelectionViewController"];
                [self.navigationController pushViewController:vc animated:YES];
            }else{
                /* 返回至首页 */
                [self.navigationController popToRootViewControllerAnimated:YES];
            }
        }else{
            [self hiddenHUDWithString:model.message error:NO];
        }
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
